<template>
	<view class="affirm">
		<view class="consignee" @click="editAddress()" v-if="address">
			<p>
				{{ address.contactUser }}
				<text class="right">{{ address.contactPhone }}</text>
			</p>
			<p class="site">
				收货地址：{{ address.province }}{{ address.city }}{{ address.region }}{{ address.addrDetail }}
				
				<uni-icons type="arrowright" size="30rpx" color="#999" class="uni-arrowright" />
			</p>
			<image src="/static/img/consignee_bg.png" />
		</view>
		<view class="consignee" @click="editAddress()" v-else>
			<p class="add">
				<image src="../../../static/img/add1.png" mode=""></image>
				添加地址
			</p>
			<image src="/static/img/consignee_bg.png" />
		</view>
		<view class="cut-off"></view>
		<view class="shop">
			<view class="list" v-for="(item, index) in inherit" :key="index">
				<view class="img">
					<image :src="item.img" class="imgthumb" mode="aspectFill" />
				</view>
				
				<view class="text">
					<p class="title">{{ item.goodsName }}</p>
					<p class="trait">颜色：{{ item.productSkuDesplist[0].split(':')[1] }}</p>
					<p class="trait">尺码：{{ item.productSkuDesplist[1].split(':')[1] }}</p>
				</view>
			</view>
			<view class="fill">
				<view class="input">
					<text>配送方式</text>
					<picker @change="bindPickerChange" :value="index" :range="array">
						<view class="uni-input">{{ array[index] }}</view>
					</picker>
					<uni-icons type="arrowdown" size="30rpx" color="#9b9b9b" class="uni-arrowdown" />
				</view>
				<view class="input">
					<text>身高</text>
					<input type="number" v-model="input.height" placeholder="输入身高cm" />
				</view>
				<view class="input">
					<text>体重</text>
					<input type="number" v-model="input.weight" placeholder="输入体重kg" />
				</view>
				<view class="input" v-if="isshow">
					<text>代理ID</text>
					<input v-model="input.agentInfo" placeholder="输入代理手机号或ID" @blur="checkTel" />
				</view>
				<view class="input" v-show="agentname">
					<text>代理名称</text>
					<view>{{agentname}}</view>
				</view>
				<view class="input">
					<text>订单备注</text>
					<input v-model="input.remark" placeholder="选填  买家留言" />
				</view>
			</view>
		</view>
		<view class="statistics">
			<text class="quantity">共{{ inherit.length }}件</text>
			合计：
			<text class="price">￥{{ money }}</text>
			<view class="sub" @click="sub()">提交订单</view>
		</view>
	</view>
</template>

<script>
import { uniIcons } from '@dcloudio/uni-ui';
import affirm from '../../../servers/order/affirm.js';
export default {
	components: { uniIcons },
	data() {
		return {
			array: [],
			index: 4,
			address: [],
			value: '',
			input: {
				agentInfo: '',
				height: '',
				weight: '',
				remark: ''
			},
			agent: {},
			isCheckOut: false,
			inherit: [],
			money: 0,
			agentname: '',
			expressName:'',
			isshow:true,
			isDoRefresh:false
		};
	},
	mounted() {
		var _this = this;
		uni.getStorage({
			key: 'shop',
			success: function(res) {
				if (res.data[0]) {
					_this.inherit = res.data;
					var money = 0;
					for (var i = 0; i < res.data.length; i++) {
						money += res.data[i].price;
					}
					_this.money = money.toFixed(2);
					console.log(_this.inherit)
				} else if (res.data.goodsId) {
					_this.inherit = [res.data];
					_this.inherit[0].productSkuDesplist = res.data.productSkuDesp.split(',');
					_this.money = res.data.price.toFixed(2);
					console.log(_this.inherit)
				} else {
					uni.showToast({
						title: '暂无商品信息',
						duration: 2000
					});
				}
				// _this.getaddress();
				_this.getDict();
			}
		});
	},
	
	onLoad() {
		
		 this.getaddress();
		this.getDict();
		this.checkOrder();
	},
	onShow() {
		this.getaddress();
		
	},
	methods: {
		getaddress() {
			
			if(!this.isDoRefresh){
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			
				affirm
					.deliverAddress()
					.then(res => {
						if(res.data.length>0){
							for (var i = 0; i < res.data.length; i++) {
								if (res.data[i].isDefault.value == 1) {
									this.address = res.data[i];
								}
								console.log(res.data[i])
							}
							console.log(this.address )
						}else{
							this.address=[]
						}
						
					})
					.catch(err => {
						console.log(new Error('请检查网络后重试'));
					});
			 }
			
				// uni.hideLoading();
		},
		// 校验是否开启订单绑定代理商功能
		checkOrder(){
			affirm
				.checkOrderAgentBind()
				.then(res => {
					if(res.data==0){
						this.isshow=false
					}else{
						this.isshow=true
					}
					
				})
				.catch(err => {
					console.log(new Error('请检查网络后重试'));
				});
		},
		getDict(){
			var params={
				dictGroup:'快递公司',
				dictGroupCode:'expressCompany',
				isEnable:1
			}
			affirm.queryDict(params).then(res => {
				
			 for(var i=0;i<res.data.records.length;i++){
				 this.array.push(res.data.records[i].dictValueDesp)
				 this.expressName=this.array[4]
			 }	
				})
				.catch(err => {
					console.log(err)
					console.log(new Error('请检查网络后重试'));
				});
		},
		editAddress() {
			this.commonality.route_baling('/pages/shop/my/address','address','order')
			this.receive();
		},
		receive() {
			var _this = this;
			uni.$once('checkedAddress', function(data) {
				console.log('监听到事件来自 checked ，携带参数为：');
				console.log(data);
				if(data){
					_this.isDoRefresh=true
					_this.address = data;
				}
				
			});
		},
		// 检查代理商是否可用
		checkTel() {
			if (!this.input.agentInfo) {
				this.agentname = ''
				this.agent = []
				return;
			}
			uni.showLoading({
				title: '匹配代理商中',
				mask: true
			});
			let data = {
				searchParam: this.input.agentInfo
			};
			affirm.bindSearch(data).then(res => {
				uni.hideLoading();
				if (res.data) {
					this.isCheckOut = true;
					this.agent = res.data;
					this.agentname = res.data.agentName
				} else {
					this.agentname = ''
					this.agent = []
					uni.showToast({
						icon: 'none',
						title: '未找到代理商，请输入正确的代理手机号或ID',
						duration: 2000
					});
					this.input.agentInfo = '';
				}
			});
		},
		sub() {
			
			// #ifdef H5
			var orderFrom = 4;
			// #endif
			// #ifdef APP-PLUS
			var orderFrom = 1;
			// #endif
			// #ifdef MP-WEIXIN
			var orderFrom = 3;
			// #endif
			console.log(this.address)
			if(this.address.length==0){
				uni.showToast({
					title:'您还没有收货地址',
					icon:'none'
				})
				return;
			}
			var shipAddr = {
					addrDetail:this.address.addrDetail,
					city:this.address.city,
					region:this.address.region,
					province:this.address.province,
					contactPhone:this.address.contactPhone,
					contactUser:this.address.contactUser,
			}
			if(this.inherit[0].from =='car'){
				
				
				var da = {
					shipAddr:shipAddr,
					agentId: this.agent.id,
					agentName: this.agent.agentName,
					agentSn: this.agent.agentSn,
					height: this.input.height,
					weight: this.input.weight,
					remark: this.input.remark,
					orderFrom: orderFrom,
					expressName:this.expressName?this.expressName:this.array[4]
				};
				uni.showLoading({
					title: '生成订单中',
					mask: true
				});
				affirm
					.cart(da)
					.then(res => {
						if(res.code==200){
							var order = {
								id: res.data.tradeId,
								price: res.data.orderTotalAmount,
								identificationCode:res.data.identificationCode
							};
							console.log(order)
							uni.setStorage({
								key: 'order',
								data: order,
								success: function() {
									uni.hideLoading();
									uni.navigateTo({
										url: '/pages/shop/order/pay'
									});
								}
							});
						}else{
							uni.showToast({
								title:res.msg,
								icon:'none'
							})
						}
						
					})
					.catch(err => {
						console.log(new Error('请检查网络后重试'));
						uni.hideLoading();
					});
			}else{
				var inherit = this.inherit.map(v => {
						var a = {
							goodsId: v.goodsId,
							goodsSn: v.goodsSn,
							productId: v.productId,
							quantity: 1
						};
						return a;
				});
				console.log(inherit)
				 console.log(this.address,!this.address)
				if(this.address.length==0){
					uni.showToast({
						title:'您还没有收货地址',
						icon:'none'
					})
					return;
				}
				if(this.inherit[0].actId){
					var da = {
						actId:this.inherit[0].actId,
						agentId: this.agent.id,
						agentName: this.agent.agentName,
						agentSn: this.agent.agentSn,
						orderType: 3,
						height: this.input.height,
						weight: this.input.weight,
						remark: this.input.remark,
						orderFrom: orderFrom,
						goods: inherit[0],
						shipAddr:shipAddr,
						expressName:this.expressName
					};
					affirm
						.actordersave(da)
						.then(res => {
							if(res.code==200){
								var order = {
									id: res.data.tradeId,
									price: res.data.orderTotalAmount,
									identificationCode:res.data.identificationCode
								};
								uni.setStorage({
									key: 'order',
									data: order,
									success: function() {
										uni.hideLoading();
										uni.navigateTo({
											url: '/pages/shop/order/pay'
										});
									}
								});
							}else{
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
							}
							
						})
						.catch(err => {
							console.log(new Error('请检查网络后重试'));
							uni.hideLoading();
						});
					
				}else{
					var da = {
						
						agentId: this.agent.id,
						agentName: this.agent.agentName,
						agentSn: this.agent.agentSn,
						orderType: 1,
						height: this.input.height,
						weight: this.input.weight,
						remark: this.input.remark,
						orderFrom: orderFrom,
						goods: inherit[0],
						shipAddr:shipAddr,
						expressName:this.expressName
					};
					console.log(da);
					
					affirm
						.rodersave(da)
						.then(res => {
							if(res.code==200){
								var order = {
									id: res.data.tradeId,
									price: res.data.orderTotalAmount,
									identificationCode:res.data.identificationCode
								};
								uni.setStorage({
									key: 'order',
									data: order,
									success: function() {
										uni.hideLoading();
										uni.navigateTo({
											url: '/pages/shop/order/pay'
										});
									}
								});
							}else{
								uni.showToast({
									title:res.msg,
									icon:'none'
								})
							}
							
						})
						.catch(err => {
							console.log(new Error('请检查网络后重试'));
							uni.hideLoading();
						});
				}
				
			
				
				
			}

		},
		bindPickerChange(e) {
			console.log('picker发送选择改变，携带值为', e.target.value);
			this.index = e.target.value;
			this.expressName=this.array[e.target.value]
		}
	},
	onUnload() {
		uni.removeStorage({
			key: 'address',
			success: function(res) {
				console.log('删除成功');
			}
		});
	}
};
</script>

<style lang="less" scoped>
.affirm {
	background: #fff;
	height: 100%;
	padding: 1rpx 0;
	.consignee {
		position: relative;
		margin: 37rpx 20rpx 41rpx;
		padding: 20rpx;
		box-shadow: 3rpx 0 20rpx rgba(197, 197, 197, 0.35);
		font-size: 28rpx;
		line-height: 69rpx;
		border-radius: 10rpx;
		overflow: hidden;
		image {
			position: absolute;
			width: 100%;
			left: 0;
			bottom: 0;
			width: 712rpx;
			height: 12rpx;
		}
		.site {
			position: relative;
			line-height: 50rpx;
			color: #999;
			padding-right: 49rpx;
			.uni-arrowright {
				position: absolute;
				top: 2rpx;
				right: 0rpx;
			}
		}
		.add {
			text-align: center;
			font-size: 28rpx;
			color: #8f8f8f;
			image{
				width: 30rpx;
				    height: 30rpx;
				    position: relative;
				    margin-right: 20rpx;
			
				    top: 4rpx;
				
			}
		}
	}
	.shop {
		margin: 20rpx;
		border-radius: 10rpx;
		box-shadow: 3rpx 0 20rpx rgba(197, 197, 197, 0.35);
		.list {
			padding: 20rpx;
			display: flex;
			.img,image{
				width: 200rpx;
				height: 200rpx;
			}
			.text {
				margin-left: 20rpx;
				line-height: 44rpx;
				.title {
					font-size: 28rpx;
					height: 88rpx;
					margin-bottom: 10rpx;
				}
				.trait {
					font-size: 24rpx;
					color: #999999;
				}
			}
		}
		.fill {
			padding: 20rpx;
			font-size: 32rpx;
			.input {
				position: relative;
				display: flex;
				line-height: 80rpx;
				text {
					width: 200rpx;
				}
				view {
					flex: 1;
				}
				view::after {
					border: none;
				}
				input {
					width: 100%;
					height: 80rpx;
					line-height: 80rpx;
					font-size: 32rpx;
					padding: 0 27rpx;
				}
				picker {
					font-size: 26rpx;
					width: 557rpx;
					height: 80rpx;
					line-height: 80rpx;
					border: 1px solid #e1e1e1;
					border-radius: 10rpx;
					padding: 0 27rpx;
					box-sizing: border-box;
					background: #ffffff;
					.uni-placeholder {
						color: #999999;
					}
				}
				.uni-arrowdown {
					position: absolute;
					right: 0;
					top: 0;
					width: 50rpx;
					pointer-events: none;
				}
			}
		}
	}
	.statistics {
		display: flex;
		padding: 0 20rpx;
		font-size: 28rpx;
		line-height: 80rpx;
		margin: 120rpx 0 30rpx;
		.quantity {
			color: #999;
			margin-right: 37rpx;
		}
		.price {
			font-size: 34rpx;
			color: #bd3c8c;
			flex: 1;
		}
		.sub {
			width: 283rpx;
			text-align: center;
			border-radius: 40rpx;
			color: #fff;
			background: linear-gradient(80deg, #d2158a, #fc6395);
			box-shadow: 3rpx 0 20rpx rgba(206, 205, 204, 0.35);
		}
	}
}
</style>
